home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 28
/
Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso
/
Aminet
/
dev
/
lang
/
fpc09905c.lha
/
fpc
/
inc
/
math.inc
< prev
next >
Wrap
Text File
|
1998-09-21
|
34KB
|
943 lines
{
$Id: math.inc,v 1.1.1.1 1998/03/25 11:18:44 root Exp $
This file is part of the Free Pascal run time library.
Copyright (c) 1993,97 by several people
member of the Free Pascal development team.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{*************************************************************************}
{ math.inc }
{*************************************************************************}
{ Copyright Abandoned, 1987, Fred Fish }
{ }
{ This previously copyrighted work has been placed into the }
{ public domain by the author (Fred Fish) and may be freely used }
{ for any purpose, private or commercial. I would appreciate }
{ it, as a courtesy, if this notice is left in all copies and }
{ derivative works. Thank you, and enjoy... }
{ }
{ The author makes no warranty of any kind with respect to this }
{ product and explicitly disclaims any implied warranties of }
{ merchantability or fitness for any particular purpose. }
{-------------------------------------------------------------------------}
{ Copyright (c) 1992 Odent Jean Philippe }
{ }
{ The source can be modified as long as my name appears and some }
{ notes explaining the modifications done are included in the file. }
{-------------------------------------------------------------------------}
{ Copyright (c) 1997 Carl Eric Codere }
{ }
{*************************************************************************}
{ This is the Motorola 680x0 specific port of the math include. }
{*************************************************************************}
{ }
{ o all reals are mapped to the single type under the motorola version }
{ }
{ What is left to do: }
{ o add support for sqrt with fixed. }
type
TabCoef = array[0..6] of Real;
const
PIO2 = 1.57079632679489661923; { pi/2 }
PIO4 = 7.85398163397448309616E-1; { pi/4 }
SQRT2 = 1.41421356237309504880; { sqrt(2) }
SQRTH = 7.07106781186547524401E-1; { sqrt(2)/2 }
LOG2E = 1.4426950408889634073599; { 1/log(2) }
SQ2OPI = 7.9788456080286535587989E-1; { sqrt( 2/pi )}
LOGE2 = 6.93147180559945309417E-1; { log(2) }
LOGSQ2 = 3.46573590279972654709E-1; { log(2)/2 }
THPIO4 = 2.35619449019234492885; { 3*pi/4 }
TWOOPI = 6.36619772367581343075535E-1; { 2/pi }
lossth = 1.073741824e9;
MAXLOG = 8.8029691931113054295988E1; { log(2**127) }
MINLOG = -8.872283911167299960540E1; { log(2**-128) }
DP1 = 7.85398125648498535156E-1;
DP2 = 3.77489470793079817668E-8;
DP3 = 2.69515142907905952645E-15;
const sincof : TabCoef = (
1.58962301576546568060E-10,
-2.50507477628578072866E-8,
2.75573136213857245213E-6,
-1.98412698295895385996E-4,
8.33333333332211858878E-3,
-1.66666666666666307295E-1, 0);
coscof : TabCoef = (
-1.13585365213876817300E-11,
2.08757008419747316778E-9,
-2.75573141792967388112E-7,
2.48015872888517045348E-5,
-1.38888888888730564116E-3,
4.16666666666665929218E-2, 0);
function int(d : real) : real;
begin
{ this will be correct since real = single in the case of }
{ the motorola version of the compiler... }
int:=real(trunc(d));
end;
function trunc(d : real) : longint;
var
l: longint;
Begin
asm
move.l d,d0 { get number }
move.l d2,-(sp) { save register }
move.l d0,d1
swap d1 { extract exp }
move.w d1,d2 { extract sign }
bclr #15,d1 { kill sign bit }
lsr.w #7,d1
and.l #$7fffff,d0 { remove exponent from mantissa }
bset #23,d0 { restore implied leading "1" }
cmp.w #BIAS4,d1 { check exponent }
blt @zero { strictly factional, no integer part ? }
cmp.w #BIAS4+32,d1 { is it too big to fit in a 32-bit integer ? }
bgt @toobig
sub.w #BIAS4+24,d1 { adjust exponent }
bgt @trunclab2 { shift up }
beq @trunclab7 { no shift (never too big) }
neg.w d1
lsr.l d1,d0 { shift down to align radix point; }
{ extra bits fall off the end (no rounding) }
bra @trunclab7 { never too big }
@trunclab2:
lsl.l d1,d0 { shift up to align radix point }
@trunclab3:
cmp.l #$80000000,d0 { -2147483648 is a nasty evil special case }
bne @trunclab6
tst.w d2 { this had better be -2^31 and not 2^31 }
bpl @toobig
bra @trunclab8
@trunclab6:
tst.l d0 { sign bit set ? (i.e. too big) }
bmi @toobig
@trunclab7:
tst.w d2 { is it negative ? }
bpl @trunclab8
neg.l d0 { negate }
bra @trunclab8
@zero:
clr.l d0 { make the whole thing zero }
bra @trunclab8
@toobig:
moveq #-1,d0 { ugh. Should cause a trap here. }
bclr #31,d0 { make it #0x7fffffff }
@trunclab8:
move.l (sp)+,d2
move.l d0,l
end;
if l = $7fffffff then
RunError(207)
else
trunc := l
end;
function abs(d : Real) : Real;
begin
if( d < 0.0 ) then
abs := -d
else
abs := d ;
end;
function frexp(x:Real; var e:Integer ):Real;
{* frexp() extracts the exponent from x. It returns an integer *}
{* power of two to expnt and the significand between 0.5 and 1 *}
{* to y. Thus x = y * 2**expn. *}
begin
e :=0;
if (abs(x)<0.5) then
While (abs(x)<0.5) do
begin
x := x*2;
Dec(e);
end
else
While (abs(x)>1) do
begin
x := x/2;
Inc(e);
end;
frexp := x;
end;
function ldexp( x: Real; N: Integer):Real;
{* ldexp() multiplies x by 2**n. *}
var r : Real;
begin
R := 1;
if N>0 then
while N>0 do
begin
R:=R*2;
Dec(N);
end
else
while N<0 do
begin
R:=R/2;
Inc(N);
end;
ldexp := x * R;
end;
function polevl(var x:Real; var Coef:TabCoef; N:Integer):Real;
{***************************